<?php
namespace LPCISII\PHP2012Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use LPCISII\PHP2012Bundle\Entity\Categories;
use LPCISII\PHP2012Bundle\Form\CategoriesType;
use LPCISII\PHP2012Bundle\Entity\Articles;
use LPCISII\PHP2012Bundle\Form\ArticlesType;
use LPCISII\PHP2012Bundle\Entity\Images;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use LPCISII\PHP2012Bundle\Entity\Recherche;
use LPCISII\PHP2012Bundle\Form\RechercheType;

class AdminController extends Controller
{
    private $categories = array(
            'articles' => 'Articles',
            'categories' => 'Catégories'
    );
    
    public function indexAction() {
        return $this->render('LPCISIIPHP2012Bundle:Admin:index.html.twig', array(
            'categories' => $this->categories,
            'selected' => 'articles'
        ));
    }
    
    public function tabAction($tab) {
        $parameters = array();
        if($tab == 'categories') {
            $em = $this->getDoctrine()->getEntityManager();
            $categories = $em->createQueryBuilder()
                            ->select('c')
                            ->from('LPCISIIPHP2012Bundle:Categories', 'c')
                            ->getQuery()
                            ->getResult();
        
            $parameters['cat'] = $categories;
        }
        
        $parameters['categories'] = $this->categories;
        $parameters['selected'] = $tab;
        
        return $this->render('LPCISIIPHP2012Bundle:Admin:index.html.twig', $parameters);
    }
    
    public function addArticleAction() {
        $a = new Articles();
        
        $request = $this->getRequest();
        $form    = $this->createForm(new ArticlesType(), $a);

        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);
            if($form->isValid()) {
                
                $data = $form->getData();
                
                $v = $this->container->get('doctrine')->getEntityManager();
                if($data->getReference() != '') {
                    $em = $this->getDoctrine()->getEntityManager();
                    $rpa = $em->getRepository('LPCISIIPHP2012Bundle:Articles');

                    $a = $rpa->findOneBy(array(
                        'reference' => $data->getReference()
                    ));
                } else {
                    $a = new Articles();
                }
                
                $a->setCategorie($data->getCategorie());
                $a->setDelais($data->getDelais());
                $a->setDescriptif($data->getDescriptif());
                $a->setNom($data->getNom());
                $a->setPoids($data->getPoids());
                $a->setPrix($data->getPrix());
                
                    foreach($data->getImages() as $image) {
                        if($image->getFichier() == null) break;
                        $img = new Images();
                        $img->setTitre($image->getTitre());
                        $img->setArticle($a);
                        $file = $image->getFichier();
                        $extension = $file->guessExtension();
                        if (!$extension) {
                            // extension cannot be guessed
                            $extension = 'bin';
                        }
                        $dir = __DIR__.'/../../../../web/bundles/lpcisiiphp2012/images';
                        $filename = rand(1, 99999).rand(1, 99999).'.'.$extension;
                        $file->move($dir, $filename);
                        $img->setUrl($filename);
                        $v->persist($img);
                    }
                
                
                $v->persist($a);
                $v->flush();

                return $this->redirect($this->generateUrl('LPCISIIPHP2012Bundle_admin', array(
                    'form' => $form->createView(),
                    'categories' => $this->categories,
                    'selected' => 'articles'
                )));
            }
        }

        return $this->render('LPCISIIPHP2012Bundle:Admin:addarticle.html.twig', array(
            'form' => $form->createView(),
            'categories' => $this->categories,
            'selected' => 'articles',
            'action' => 'Ajouter'
        ));
    }
    
    public function modArticleAction($reference) {
        $em = $this->getDoctrine()->getEntityManager();
        $rpa = $em->getRepository('LPCISIIPHP2012Bundle:Articles');
        
        $article = $rpa->findOneBy(array(
           'reference' => $reference 
        ));
        
        $form    = $this->createForm(new ArticlesType(), $article);
        
        return $this->render('LPCISIIPHP2012Bundle:Admin:addarticle.html.twig', array(
            'form' => $form->createView(),
            'categories' => $this->categories,
            'selected' => 'articles',
            'action' => 'Modifier'
        ));
    }
    
    public function searchArticleAction() {
        $recherche = new Recherche();
        $form = $this->createForm(new RechercheType(), $recherche);
        
        $request = $this->getRequest();
        
        if ($request->getMethod() == 'POST') {
            
            $form->bindRequest($request);

            if ($form->isValid()) {
                
                $em = $this->getDoctrine()->getEntityManager();
                $rpa = $em->getRepository('LPCISIIPHP2012Bundle:Articles');
                $nom = $form->getData()->getNomArticle();
                
                $articles = $rpa->findByNom($nom);
                
                return $this->render('LPCISIIPHP2012Bundle:Admin:index.html.twig', array(
                   'form' => $form->createView(),
                   'articles' => $articles,
                   'categories' => $this->categories,
                   'message' => 'Résultats de la recherche sur "'.$nom.'"',
                   'mode' => 'short',
                   'selected' => 'articles'
                ));
            }
        }
        return $this->render('LPCISIIPHP2012Bundle:Admin:recherche.html.twig', array(
            'form' => $form->createView(),
            'categories' => $this->categories,
            'selected' => 'articles'
        ));
    }
    
    public function delArticleAction($reference) {
        $em = $this->getDoctrine()->getEntityManager();
        $img_art = $em->getRepository('LPCISIIPHP2012Bundle:Images')
                ->findBy(array(
                    'article' => $reference
                ));
        $v = $this->container->get('doctrine')->getEntityManager();      
        foreach($img_art as $img) {
           $v->remove($img); 
        }
        
        $article = $em->getRepository('LPCISIIPHP2012Bundle:Articles')
            ->findOneBy(array(
                'reference' => $reference
            ));
        
        $v->remove($article);
        $v->flush();

        return $this->render('LPCISIIPHP2012Bundle:Admin:index.html.twig', array(
            'categories' => $this->categories,
            'selected' => 'articles'
        ));
    }
    
    public function addCategoryAction() {
        $c = new Categories();
        $action = 'Ajouter';
        $request = $this->getRequest();
        $form    = $this->createForm(new CategoriesType(), $c);
        $v = $this->container->get('doctrine')->getEntityManager();
        $rpc = $v->getRepository('LPCISIIPHP2012Bundle:Categories');
        
        if($request->getMethod() == 'POST') {
            $form->bindRequest($request);
            if($form->isValid()) {
                $data = $form->getData();
                if($data->getIdCa() != '') {
                    $c = $rpc->findOneBy(array(
                        'idCa' => $data->getIdCa()
                    ));
                    $action = 'Modifier';
                } else {
                    $c = new Categories();
                }
                
                $c->setNom($data->getNom());
                $v->persist($c);
                $v->flush();
            }
        }
        
        return $this->render('LPCISIIPHP2012Bundle:Admin:addcategorie.html.twig', array(
           'selected' => 'categories',
            'categories' => $this->categories,
            'action' => $action,
            'form' => $form->createView()
        ));
    }
    
    public function delCategoryAction($idCa) {
        $v = $this->container->get('doctrine')->getEntityManager();
        $rpc = $v->getRepository('LPCISIIPHP2012Bundle:Categories');
        
        $category = $rpc->findOneBy(array(
           'idCa' => $idCa 
        ));
        
        $articles = $category->getArticles(); 
        foreach($articles as $article) {
            $img_art = $article->getImages();
                 
            foreach($img_art as $img) {
                $v->remove($img); 
            }

            $v->remove($article);
            
        }
        $v->remove($category);
        $v->flush();
        
        return $this->tabAction('categories');
    }
    
    public function modCategoryAction($idCa) {
        $em = $this->getDoctrine()->getEntityManager();
        $rpc = $em->getRepository('LPCISIIPHP2012Bundle:Categories');
        
        $category = $rpc->findOneBy(array(
           'idCa' => $idCa 
        ));
        
        $form    = $this->createForm(new CategoriesType(), $category);
        
        return $this->render('LPCISIIPHP2012Bundle:Admin:addcategorie.html.twig', array(
            'form' => $form->createView(),
            'categories' => $this->categories,
            'selected' => 'categories',
            'action' => 'Modifier'
        ));
    }
}
